package ext.acae2.workflow;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import wt.fc.PersistenceHelper;
import wt.fc.PersistenceServerHelper;
import wt.fc.QueryResult;
import wt.fc.ReferenceFactory;
import wt.org.WTUser;
import wt.pds.StatementSpec;
import wt.query.ClassAttribute;
import wt.query.KeywordExpression;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTException;
import wt.vc.baseline.ManagedBaseline;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfBlock;
import wt.workflow.engine.WfEngineHelper;
import wt.workflow.engine.WfProcess;
import wt.workflow.engine.WfState;
import wt.workflow.work.WfAssignedActivity;
import wt.workflow.work.WfAssignment;
import wt.workflow.work.WorkItem;
import ext.acae2.utilities.principal.CSCPrincipal;
import ext.acae2.utilities.util.CSCUtil;

public class WorkflowHelper {
	
	public static WfBlock getBlock(WfProcess wfprocess) throws Exception {
    	WfProcess parentProcess = null;
    	WfBlock wfblock = null;
    	QuerySpec queryspec = new QuerySpec(WfBlock.class);
        QueryResult queryresult = PersistenceServerHelper.manager.query(queryspec);
        while(queryresult.hasMoreElements()){
        	WfBlock tmpblock = (WfBlock)queryresult.nextElement();
        	parentProcess = tmpblock.getParentProcess();
        	if(parentProcess.equals(wfprocess))
        		return tmpblock;
        }
        return wfblock;
        
    }
	
	public static List getActivities(WfBlock wfblock,List activityList) throws WTException{
    	Enumeration enumeration = WfEngineHelper.service.getProcessSteps(wfblock,null);
    	while(enumeration.hasMoreElements())
    	{
    		WfActivity wfactivity = (WfActivity)enumeration.nextElement();
    		if(wfactivity instanceof WfAssignedActivity)
    		{
    			WfAssignedActivity wfassignedactivity = (WfAssignedActivity)wfactivity;
    			activityList.add(wfassignedactivity);
    		}
    	}
    	return activityList;
    }
	
	public static List getActivities(WfProcess wfprocess,List activityList) throws WTException{
    	Enumeration enumeration = WfEngineHelper.service.getProcessSteps(wfprocess,null);
    	while(enumeration.hasMoreElements())
    	{
    		WfActivity wfactivity = (WfActivity)enumeration.nextElement();
    		if(wfactivity instanceof WfAssignedActivity)
    		{
    			WfAssignedActivity wfassignedactivity = (WfAssignedActivity)wfactivity;
    			activityList.add(wfassignedactivity);
    		}
    	}
    	return activityList;
    }
	
	public static List<WTUser> getWorkItemCompleteBy(WfAssignedActivity wfassignedActivity) throws WTException{
		//System.out.println("start==========>"+wfassignedActivity.getName());
		List<WTUser> users = new ArrayList<WTUser>();
		Enumeration<WfAssignment> wfassignments = wfassignedActivity.getAssignments();
		if(wfassignments.hasMoreElements()){
			WfAssignment assignment = wfassignments.nextElement();
			ReferenceFactory rf = new ReferenceFactory();
			String assignmentOid = rf.getReference(assignment).toString();
			String identity = assignmentOid.substring(assignmentOid.indexOf(":") + 1);
			QuerySpec qs = new QuerySpec(WorkItem.class);
			qs.setAdvancedQueryEnabled(true);
//			ClassAttribute clsAttr = new ClassAttribute(WorkItem.class,
//					"parentWA.key.id");
//			qs.appendWhere(new SearchCondition(WorkItem.class, ManagedBaseline.NUMBER,
//					SearchCondition.LIKE, baselineNum + "%",false), new int[]{INDEX});
//			qs.appendWhere(new SearchCondition())
			qs.appendSearchCondition(new SearchCondition(new KeywordExpression(
					"idA3C4"), SearchCondition.EQUAL, new KeywordExpression(
							identity)));
			QueryResult qr = PersistenceHelper.manager.find((StatementSpec) qs);
			while (qr.hasMoreElements()) {
				WorkItem wi = (WorkItem) qr
						.nextElement();
				String userName = wi.getCompletedBy();
				//System.out.println("username=====================>"+userName);
				WTUser user = CSCPrincipal.getUserByName(userName);
				users.add(user);
			}
		}
		return users;
	}
	
	public static void main(String[] args) throws WTException {
		ManagedBaseline baseline = (ManagedBaseline) CSCUtil.getObjectByOid("OR:wt.vc.baseline.ManagedBaseline:260082");
		QueryResult processqr = WfEngineHelper.service.getAssociatedProcesses(baseline, WfState.CLOSED_COMPLETED, baseline.getContainerReference());
		List<WfActivity> activityList = new ArrayList<WfActivity>();
		if(processqr.hasMoreElements()){
			WfProcess process = (WfProcess)processqr.nextElement();
			activityList = WorkflowHelper.getActivities(process, activityList);
		}
		for(WfActivity activity:activityList){
			getWorkItemCompleteBy((WfAssignedActivity) activity);
		}
	}

}
